import request from '@/config/axios'

export interface DataSourceConfigVO {
  id: number | undefined
  connectName: string
  host: string
  port: string
  dbName: string
  username: string
  password: string
  maxFileNum: number | undefined
  createTime?: Date
}
export interface DbRestoreVO {
  id: number 
  fileName: string
  targetId: number
}
export interface DbBackupTaskVO {
  id: number 
  cron: string
}

// 新增数据源配置
export const createDataSourceConfig = (data: DataSourceConfigVO) => {
  return request.post({ url: '/cms/db-backup/database', data })
}

// 修改数据源配置
export const updateDataSourceConfig = (data: DataSourceConfigVO) => {
  return request.post({ url: '/cms/db-backup/database', data })
}

// 删除数据源配置
export const deleteDataSourceConfig = (id: number) => {
  return request.delete({ url: '/cms/db-backup/database/delete?id=' + id })
}

// 查询数据源配置详情
export const getDataSourceConfig = (id: number) => {
  return request.get({ url: '/cms/db-backup/database/' + id })
}

// 查询数据源配置列表
export const getDataSourceConfigList = () => {
  return request.get({ url: '/cms/db-backup/database' })
}

// 查询数据库恢复可选择列表
export const getDbListByType = (dbType:string) => {
  return request.get({ url: '/cms/db-backup/database/selection?dbType='+dbType })
}

//备份数据源
export const dbBackup = (id: number) => {
  return request.post({ url: '/cms/db-backup/backup/' + id })
}

//还原数据源
export const dbRestore = (data: DbRestoreVO) => {
  return request.post({ url: '/cms/db-backup/restore' , data })
}

//根据数据库id，获取备份文件
export const getDbBackupFiles = (id: number) => {
  return request.get({ url: '/cms/db-backup/file/' + id })
}

// 删除数据库备份文件
export const deleteDbBackupFile = (id: number,fileName: string) => {
  return request.delete({ url: '/cms/db-backup/file?id=' + id +'&fileName='+fileName })
}
// 下载数据库备份文件
export const downloadDbBackupFile = (id: number,fileName: string) => {
  return request.download({ url: '/cms/db-backup/file/download?id=' + id +'&fileName='+fileName })
}
// 查询数据备份定时任务
export const getTaskById = (id: number) => {
  return request.get({ url: '/cms/db-backup/task/' + id })
}
//创建或者修改数据备份定时任务
export const saveTask = (data: DbBackupTaskVO) => {
  return request.post({ url: '/cms/db-backup/task/' , data })
}
//删除数据备份定时任务
export const deleteTask = (id: number) => {
  return request.delete({ url: '/cms/db-backup/task/delete?id=' +id })
}
